###########################################################################
#
#   Copyright 2010 American Public Media Group
#
#   This file is part of AIR2.
#
#   AIR2 is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   AIR2 is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
#
###########################################################################

package AIR2::Utils;
use strict;
use warnings;
use Carp;
use Digest::MD5 qw( md5_hex );

sub logger { print "[" . localtime() . "] [$$] " . join( ' ', @_ ) }

sub random_str {
    my $self = shift;
    my $n = shift || 12;
    if ( $n =~ m/\D/ ) {
        croak "error: 'n' must be a positive integer, not $n";
    }
    my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9 );
    return join( "", @chars[ map { rand @chars } ( 1 .. $n ) ] );
}

sub str_to_uuid {
    my $self = shift;
    my $str  = shift;
    my $len  = shift || 12;
    my $uuid = substr( md5_hex($str), 0, $len );
    return $uuid;
}

1;
